package com.play.parse.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.play.parse.JsoupHandler;

import edu.uci.ics.crawler4j.parser.ExtractedUrlAnchorPair;

public class AimerHandler extends JsoupHandler{
	protected static final Logger logger = Logger.getLogger(AimerHandler.class.getName()); 
	
	/**
	 * 商品网
	 */
	protected void productSeed(Document doc) {
		Elements links = doc.select("a[href]");
		if(null != links && links.size() != 0){
			for (Element element : links) {
				ExtractedUrlAnchorPair curUrl = new ExtractedUrlAnchorPair();
				String seedurl = element.attr("abs:href");
				curUrl.setHref(seedurl);
				outgoingurls.add(curUrl);
			}
		}
	}

	protected void productList(Document doc) {
		Elements links = doc.select("div.cattieproduct");
		if(null != links && 0 != links.size()) {
			List<String> prodects = new ArrayList<String>();
			for (Element src : links) {
				Elements price = src.select("p.original > span.track,p.name > a");
				int i = 0;
				String pricestr = "";
				String link = "";
				for(Element abshref : price) {
					if(abshref.tagName().equals("span")) {
						pricestr = abshref.text();
					} else {
						link = abshref.attr("abs:href");
						ExtractedUrlAnchorPair curUrl = new ExtractedUrlAnchorPair();
						curUrl.setHref(link);
						outgoingurls.add(curUrl);
					}
					++ i;
					if(!"".equals(pricestr) && !"".equals(link) && (i % 2 == 0)) {
						prodects.add(link + " ~ " + pricestr + "\n");
						link = pricestr = "";
					}
				}
			}
			
			try {
				FileUtils.writeLines(new File(filePath+File.separator+getClass().getSimpleName()+File.separator +"price.txt"), prodects,true);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		
		//获取分页信息
		Elements pages = doc.select("div.listscroll a");
		for (Element page : pages) {
			String pageUrl = page.attr("abs:href");
			//logger.info(pageUrl);
			ExtractedUrlAnchorPair curUrl = new ExtractedUrlAnchorPair();
			curUrl.setHref(pageUrl);
			outgoingurls.add(curUrl);
		}
	}

	protected void productInfo(String contextURL, Document doc) {
		Elements info = doc.select("a.showcart");
		if(info != null && info.size() != 0) {
			StringBuilder prodect = new StringBuilder(contextURL);
			prodect.append(" ~ ");
			
			//价格无法获取
			/*Elements price = doc.select("span[style]");
			prodect.append(price.first().text()).append(" ~ ");
			System.out.println(price.first().text());*/
			//获取位置（类别路径）信息
			Elements path = doc.select("div.categorybread");
			prodect.append(path.text()).append(" ~ ");
			
			//产品名称，不同类别不一样
			//Elements name = doc.select("div.p_info h2,div.p_info h1");
			Elements name = doc.getElementsByTag("title");
			prodect.append(name.text()).append(" ~ ");
			//获取产品信息
			Elements atrr = doc.select("div.goodsjuli");
			prodect.append(atrr.text());
			prodect.append("\n");
			
			try {
				FileUtils.write(new File(filePath+File.separator+getClass().getSimpleName()+File.separator+"productInfo.txt"), prodect, true);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			
		}
	}

	@Override
	protected String getFilePath() {
		return  "crawlerdata";
	}
	
}
